package com.atbun.eduservice.service.impl;

import com.atbun.eduservice.entity.EduCourse;
import com.atbun.eduservice.entity.EduTeacher;
import com.atbun.eduservice.entity.vo.TeacherQuery;
import com.atbun.eduservice.mapper.EduTeacherMapper;
import com.atbun.eduservice.service.EduCourseService;
import com.atbun.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-01
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByAsc("sort");//排序
//        如果查询条件为空
        if (null == teacherQuery){
            baseMapper.selectPage(pageParam,queryWrapper);
            return;
        }
//        名字模糊查询
        queryWrapper.like(StringUtils.isNotBlank(teacherQuery.getName()),"name",teacherQuery.getName());
//        精确查询
        queryWrapper.eq(teacherQuery.getLevel() != null,"level",teacherQuery.getLevel());
//        大于 小于等于
        queryWrapper.ge(teacherQuery.getBegin() != null,"gmt_create",teacherQuery.getBegin());
        queryWrapper.le(teacherQuery.getEnd() != null,"gmt_create",teacherQuery.getEnd());

        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 客户端讲师分页列表数据
     * @param parman
     * @return
     */
    @Override
    public Map<String, Object> pageTeacher(Page<EduTeacher> parman) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        this.baseMapper.selectPage(parman,queryWrapper);

        List<EduTeacher> records = parman.getRecords(); //数据集合
        long current = parman.getCurrent(); // 当前页
        long size = parman.getSize(); // 每页显示条数
        long total = parman.getTotal(); // 总条数
        long pages = parman.getPages(); // 总页数
        boolean hasNext = parman.hasNext(); // 下一页
        boolean hasPrevious = parman.hasPrevious(); // 上一页

        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

    /**
     * 根据讲师id查询讲师详情。
     * @param id
     * @return
     */
    @Override
    public EduTeacher getTeacherInfo(String id) {
//        查询讲师的基本信息
        QueryWrapper<EduTeacher> queryTeacher = new QueryWrapper<>();
        queryTeacher.eq("id",id);
        EduTeacher eduTeacher = this.baseMapper.selectOne(queryTeacher);

        return eduTeacher;
    }
}
